//
//  Contacts.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/19/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit

final class ContactsViewController: UIViewController, Alertable {
    
    // MARK: - Outlets

     private lazy var submitBtn: UIButton = {
        let btn = UIButton()
        btn.clipsToBounds = true
        btn.layer.cornerRadius = 15
        btn.setTitle("Далее", for: .normal)
        btn.backgroundColor = .blueColor
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel!.font = UIFont.systemFont(ofSize: 17, weight: .medium)
        return btn
    }()

    private lazy var nameField: ModalElement = {
        let element = ModalElement()
        element.updateUI(value: nil, placeholder: "Имя", withTF: true, keyboardType: .asciiCapable)
        return element
    }()
    
    private lazy var emailField: ModalElement = {
        let element = ModalElement()
        element.updateUI(value: nil, placeholder: "Почта", withTF: true, keyboardType: .emailAddress)
        return element
    }()
    
    private lazy var cityBtn: ModalElement = {
        let element = ModalElement()
        element.updateUI(value: nil, placeholder: "Город продажи")
        element.delegate = self
        return element
    }()
    
    private lazy var buttonsStack: UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.alignment = .fill
        stack.distribution = .fillEqually
        stack.spacing = 12
        return stack
    }()

    // MARK: - Props
    
    public weak var delegate: AdCreatorProtocol!
    
    private var city: City? {
        didSet {
            cityBtn.updateUI(value: city?.name)
        }
    }
    
    
    // MARK: - UI Actions
    
    @objc private func goBack (sender: UIBarButtonItem) {
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc private func closeVC (sender: UIBarButtonItem) {
        self.navigationController?.dismiss(animated: true, completion: nil)
        delegate.removeEverything()
    }
    
    @objc private func onSubmit (sender: UIBarButtonItem) {
        if city == nil || nameField.getTextFieldValue().isEmpty || emailField.getTextFieldValue().isEmpty {
            self.showAlert("Упс...", "Заполните все поля")
        } else {
            delegate.getContacts(name: nameField.getTextFieldValue(), email: emailField.getTextFieldValue(), city: city)
            let vc = PriceViewController()
            vc.delegate = self.delegate
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }

    // MARK: - Life-Cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUpUI()
        self.setUpNavBar()
    }

}

// MARK: - Methods


extension ContactsViewController: RegionViewControllerDelegate {
    func selectCity(city: [City]) {
        self.city = city.item(at: 0)
    }
}

// MAKR: - UI

extension ContactsViewController {
    
    private func setUpUI () {
        view.backgroundColor = .white
        self.view.addSubview(buttonsStack)
        self.view.addSubview(submitBtn)
        
        buttonsStack.addArrangedSubview(nameField)
        buttonsStack.addArrangedSubview(emailField)
        buttonsStack.addArrangedSubview(cityBtn)
        
        submitBtn.addTarget(self, action: #selector(onSubmit(sender:)), for: .touchUpInside)
        
        self.addConstraints()
        self.addCloseIcon()
    }
    
    private func addConstraints () {
        let itemsCount = buttonsStack.arrangedSubviews.count
        
        buttonsStack.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalTo(view.safeAreaLayoutGuide).offset(30)
            make.height.equalTo(itemsCount * 48 + (itemsCount - 1) * 12)
        }
        
        submitBtn.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.bottom.equalTo(view.safeAreaLayoutGuide).offset(-16)
            make.height.equalTo(44)
        }
    }
    
    private func setUpNavBar () {
        let backIcon = UIImage(named: "cancel")
        let backButton = UIBarButtonItem(image: backIcon, landscapeImagePhone: backIcon, style: .plain, target: self, action: #selector(goBack(sender:)))
        backButton.tintColor = .greyColor
        
        self.navigationItem.simpleNavBar(title: "Контакты", backButton: backButton)
        navigationItem.largeTitleDisplayMode = .always
        navigationController?.navigationBar.largeTitleTextAttributes = [
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 24, weight: .bold)
        ]
        navigationItem.hidesSearchBarWhenScrolling = false
        self.navigationController?.navigationItem.largeTitleDisplayMode = .always
    }

    private func addCloseIcon () {
        let closeBarBnt = UIBarButtonItem(image: UIImage(named: "dismiss"), style: .plain, target: self, action: #selector(closeVC))
        closeBarBnt.tintColor = .greyColor
        self.navigationItem.rightBarButtonItem = closeBarBnt
    }
    
}

extension ContactsViewController: ModalElementProtocol {
    func selectElement(modal: ModalView.ModalType?, customID: Int?) {
        let regionVC = RegionViewController()
        regionVC.delegate = self
        let nvc = UINavigationController(rootViewController: regionVC)
        nvc.modalPresentationStyle = .fullScreen
        self.navigationController?.pushViewController(regionVC, animated: true)
    }
    
    func didClear(modal: ModalView.ModalType) {
        //
    }
}




